{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "573391cd",
   "metadata": {},
   "source": [
    "Excel表格“pydata04”收集了某城市的天气数据，基于\"pydata04.xlsx”数据集回答以下问题，数据导入后命名为pydata04。以下每道题均是在原始数据基础上进行的，题干所涉及到的数据修改操作不继承到其他题中pydata04.xlsx"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1cd5024b",
   "metadata": {},
   "source": [
    "#### 101、求出pydata04.xlsx列名为'相对湿度’的中位数 (A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "1fd8e562",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>日期</th>\n",
       "      <th>大气温度</th>\n",
       "      <th>气象站气压</th>\n",
       "      <th>相对湿度</th>\n",
       "      <th>类别</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2023-02-22 14:00:00</td>\n",
       "      <td>17.9</td>\n",
       "      <td>765.9</td>\n",
       "      <td>54</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2023-02-22 11:00:00</td>\n",
       "      <td>19.2</td>\n",
       "      <td>768.1</td>\n",
       "      <td>54</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2023-02-22 08:00:00</td>\n",
       "      <td>14.8</td>\n",
       "      <td>767.6</td>\n",
       "      <td>66</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2023-02-22 05:00:00</td>\n",
       "      <td>14.4</td>\n",
       "      <td>766.8</td>\n",
       "      <td>73</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2023-02-22 02:00:00</td>\n",
       "      <td>15.1</td>\n",
       "      <td>767.2</td>\n",
       "      <td>75</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   日期  大气温度  气象站气压  相对湿度  类别\n",
       "0 2023-02-22 14:00:00  17.9  765.9    54   1\n",
       "1 2023-02-22 11:00:00  19.2  768.1    54   2\n",
       "2 2023-02-22 08:00:00  14.8  767.6    66   3\n",
       "3 2023-02-22 05:00:00  14.4  766.8    73   1\n",
       "4 2023-02-22 02:00:00  15.1  767.2    75   3"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "df = pd.read_excel(\"pydata04_1678066099340.xlsx\")\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "cdd1b80e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "84.5"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['相对湿度'].median()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b1b9faec",
   "metadata": {},
   "source": [
    "#### 102、在使用to_csv函数方法导出数据集pydata04时index参数的默认值为 (D)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "716c1c26",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 查看函数用法\n",
    "df.to_csv?"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4748e0b4",
   "metadata": {},
   "source": [
    "#### 103、请使用shape()方法，查看数据表的列数为 (C)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "82a66d27",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(170, 5)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1291e833",
   "metadata": {},
   "source": [
    "#### 104、pydata04中所有类别为2的样本中大气温度的1/4分位数为 (B)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "99c8ef2a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>大气温度</th>\n",
       "      <th>气象站气压</th>\n",
       "      <th>相对湿度</th>\n",
       "      <th>类别</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>59.000000</td>\n",
       "      <td>59.000000</td>\n",
       "      <td>59.000000</td>\n",
       "      <td>59.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>18.435593</td>\n",
       "      <td>762.601695</td>\n",
       "      <td>83.474576</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>2.230646</td>\n",
       "      <td>2.473131</td>\n",
       "      <td>13.566383</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>12.000000</td>\n",
       "      <td>758.900000</td>\n",
       "      <td>52.000000</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>17.250000</td>\n",
       "      <td>760.700000</td>\n",
       "      <td>74.000000</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>18.100000</td>\n",
       "      <td>762.000000</td>\n",
       "      <td>86.000000</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>19.450000</td>\n",
       "      <td>763.550000</td>\n",
       "      <td>95.000000</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>26.000000</td>\n",
       "      <td>769.500000</td>\n",
       "      <td>100.000000</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            大气温度       气象站气压        相对湿度    类别\n",
       "count  59.000000   59.000000   59.000000  59.0\n",
       "mean   18.435593  762.601695   83.474576   2.0\n",
       "std     2.230646    2.473131   13.566383   0.0\n",
       "min    12.000000  758.900000   52.000000   2.0\n",
       "25%    17.250000  760.700000   74.000000   2.0\n",
       "50%    18.100000  762.000000   86.000000   2.0\n",
       "75%    19.450000  763.550000   95.000000   2.0\n",
       "max    26.000000  769.500000  100.000000   2.0"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "condition = (df['类别'] == 2)\n",
    "df[condition].describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "12d199f2",
   "metadata": {},
   "source": [
    "#### 105、请通过pydata04.xlsx，求类别为2的平均相对湿度，其结果为 ( B)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "728e32c2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "83.47457627118644"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "condition = (df['类别'] == 2)\n",
    "df[condition]['相对湿度'].mean()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3c2578a4",
   "metadata": {},
   "source": [
    "#### 106、下列能够正确筛选出2023年2月18日5点整的数据的语句是 (A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "84b1e5b3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>日期</th>\n",
       "      <th>大气温度</th>\n",
       "      <th>气象站气压</th>\n",
       "      <th>相对湿度</th>\n",
       "      <th>类别</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>165</th>\n",
       "      <td>2023-02-01 14:00:00</td>\n",
       "      <td>21.2</td>\n",
       "      <td>760.7</td>\n",
       "      <td>74</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>166</th>\n",
       "      <td>2023-02-01 11:00:00</td>\n",
       "      <td>21.8</td>\n",
       "      <td>762.7</td>\n",
       "      <td>66</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>167</th>\n",
       "      <td>2023-02-01 08:00:00</td>\n",
       "      <td>17.1</td>\n",
       "      <td>761.9</td>\n",
       "      <td>83</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>168</th>\n",
       "      <td>2023-02-01 05:00:00</td>\n",
       "      <td>16.6</td>\n",
       "      <td>760.7</td>\n",
       "      <td>83</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>169</th>\n",
       "      <td>2023-02-01 02:00:00</td>\n",
       "      <td>17.1</td>\n",
       "      <td>760.8</td>\n",
       "      <td>80</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     日期  大气温度  气象站气压  相对湿度  类别\n",
       "165 2023-02-01 14:00:00  21.2  760.7    74   2\n",
       "166 2023-02-01 11:00:00  21.8  762.7    66   1\n",
       "167 2023-02-01 08:00:00  17.1  761.9    83   2\n",
       "168 2023-02-01 05:00:00  16.6  760.7    83   2\n",
       "169 2023-02-01 02:00:00  17.1  760.8    80   1"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.tail()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "41e483be",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>日期</th>\n",
       "      <th>大气温度</th>\n",
       "      <th>气象站气压</th>\n",
       "      <th>相对湿度</th>\n",
       "      <th>类别</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>35</th>\n",
       "      <td>2023-02-18 05:00:00</td>\n",
       "      <td>16.7</td>\n",
       "      <td>763.2</td>\n",
       "      <td>88</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                    日期  大气温度  气象站气压  相对湿度  类别\n",
       "35 2023-02-18 05:00:00  16.7  763.2    88   3"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 要么把日期当作字符串\n",
    "df = pd.read_excel(\"pydata04_1678066099340.xlsx\")\n",
    "df[df['日期'] == \"2023-02-18 05:00:00\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "b8634c6c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>日期</th>\n",
       "      <th>大气温度</th>\n",
       "      <th>气象站气压</th>\n",
       "      <th>相对湿度</th>\n",
       "      <th>类别</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>35</th>\n",
       "      <td>2023-02-18 05:00:00</td>\n",
       "      <td>16.7</td>\n",
       "      <td>763.2</td>\n",
       "      <td>88</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                    日期  大气温度  气象站气压  相对湿度  类别\n",
       "35 2023-02-18 05:00:00  16.7  763.2    88   3"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 可以但不规范做法\n",
    "df[df['日期'] == \"2023/2/18 05:00:00\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "84df3492",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>日期</th>\n",
       "      <th>大气温度</th>\n",
       "      <th>气象站气压</th>\n",
       "      <th>相对湿度</th>\n",
       "      <th>类别</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>35</th>\n",
       "      <td>2023-02-18 05:00:00</td>\n",
       "      <td>16.7</td>\n",
       "      <td>763.2</td>\n",
       "      <td>88</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                    日期  大气温度  气象站气压  相对湿度  类别\n",
       "35 2023-02-18 05:00:00  16.7  763.2    88   3"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 要么把字符串日期转成日期格式\n",
    "df['日期'] = pd.to_datetime(df['日期'])\n",
    "df[df['日期'] == pd.Timestamp(\"2023-02-18 05:00:00\")]  # 规范做法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "d66dd515",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>日期</th>\n",
       "      <th>大气温度</th>\n",
       "      <th>气象站气压</th>\n",
       "      <th>相对湿度</th>\n",
       "      <th>类别</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>35</th>\n",
       "      <td>2023-02-18 05:00:00</td>\n",
       "      <td>16.7</td>\n",
       "      <td>763.2</td>\n",
       "      <td>88</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                    日期  大气温度  气象站气压  相对湿度  类别\n",
       "35 2023-02-18 05:00:00  16.7  763.2    88   3"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import time\n",
    "import datetime\n",
    "# 可以但不规范做法，严格来说日期现在是pd.Timestamp格式，不是datetime格式\n",
    "df[df['日期'] == datetime.datetime.strptime('2023/2/18 05-00-00', '%Y/%m/%d %H-%M-%S')]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
